package com.dhcc.sds.standard.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.*;
import com.dhcc.cdc.busi.entity.CdcLabEntryEntity;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandDicsubMapper;
import com.dhcc.sds.standard.dao.SdsStandLabitemMatchMapper;
import com.dhcc.sds.standard.dao.SdsStandMatchLogMapper;
import com.dhcc.sds.standard.dto.CdcLabOrderDto;
import com.dhcc.sds.standard.dto.CdcLabitemDto;
import com.dhcc.sds.standard.entity.*;
import com.dhcc.sds.standard.service.ISdsStandDicMatchParamService;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import com.dhcc.sds.standard.service.ISdsStandDicsubService;
import com.dhcc.sds.standard.service.ISdsStandLabitemMatchService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 标准化_检验项目配置表数据服务层接口
 * 
 * @ClassName: SdsStandLabitemMatchServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:53
 */
@Service("sdsStandLabitemMatchService")
public class SdsStandLabitemMatchServiceImpl extends BaseServiceImpl<SdsStandLabitemMatchMapper, SdsStandLabitemMatchEntity> implements ISdsStandLabitemMatchService {

	@Autowired
	private SdsStandDicsubMapper standDicsubMapper;

	@Autowired
	private ISdsStandDicService sdsStandDicService;

	@Autowired
	private ISdsStandDicsubService sdsStandDicsubService;

	@Autowired
	private CdcLabItemMapper cdcLabitemMapper;

	@Autowired
	private CdcLabSetMapper cdcLabsetMapper;

	@Autowired
	private CdcLabOrderMapper cdcLabOrderMapper;

	@Autowired
	private CdcDictionaryMapper cdcDictionaryMapper;

	@Autowired
	private CdcLabEntryMapper cdcLabEntryMapper;

	@Autowired
	private SdsStandMatchLogMapper sdsStandMatchLogMapper;

	@Autowired
	private CdcOrdmastMapper cdcOrdmastMapper;

	@Autowired
	private ISdsStandDicMatchParamService sdsStandDicMatchParamService;

	@Override
	public List<SdsStandLabitemMatchEntity> page(Page<SdsStandLabitemMatchEntity> page, Map<String, Object> map, String compare) {
		List<SdsStandLabitemMatchEntity> page1 = this.baseMapper.page(page, map);
		Object dicSubId = map.get("dicSubId");
		for (SdsStandLabitemMatchEntity standLabitemMatchEntity : page1){
			if(standLabitemMatchEntity.getLabitemId() != null){
				CdcLabItemEntity labitemEntity = cdcLabitemMapper.selectById(standLabitemMatchEntity.getLabitemId());
				standLabitemMatchEntity.setLabitemDesc(labitemEntity.getDescription());
			}

			if (standLabitemMatchEntity.getDicsubId() != null){
				SdsStandDicsubEntity dicsubEntity = standDicsubMapper.selectById(standLabitemMatchEntity.getDicsubId());
				standLabitemMatchEntity.setDicsubDesc(dicsubEntity.getDicDesc());
				//多选框赋值
				if ((dicSubId != null) && (!"".equals(dicSubId))){
					Long aLong = Long.valueOf(String.valueOf(dicSubId));
					if (aLong.equals(standLabitemMatchEntity.getDicsubId())){
						standLabitemMatchEntity.setIsCheck(true);
					}
				}
			}


		}

		List<SdsStandLabitemMatchEntity> collect = new ArrayList<>();
		if("yes".equals(compare)){
			collect = page1.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
			return collect;
		}else if("no".equals(compare)){
			collect = page1.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
			return collect;
		}else {
			return page1;
		}
	}

	@Override
	public List<SdsStandLabitemMatchEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public void sync() {
		//查询检验项目数据
		List<CdcLabItemEntity> cdcLabitemEntities = cdcLabitemMapper.selectList(null);
		//查询标准化-检验项目配置表数据
		List<SdsStandLabitemMatchEntity> sdsStandLabitemMatchEntities = this.baseMapper.selectList(null);

		//取两个list差集
		List<CdcLabItemEntity> collect = cdcLabitemEntities.stream()
				.filter(item -> !sdsStandLabitemMatchEntities.stream()
						.map(e -> e.getLabitemId())
						.collect(Collectors.toList())
						.contains(item.getId()))
				.collect(Collectors.toList());

		//插入数据
		for (CdcLabItemEntity labitemEntity : collect){
			SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
			standLabitemMatchEntity.setLabitemId(labitemEntity.getId());
			this.baseMapper.insert(standLabitemMatchEntity);
		}

	}

	@Override
	public void compare(Long sdsStandDicsubId, String labitemIds, Long sdsStandDicId, String laborderIds, String username) {
		/*if (!StringUtils.isEmpty(sdsStandDicsubId)){
			//标准字典子项关联检验项目
			String[] split1 = labitemIds.split(",");
			for (int i = 0; i < split1.length; i++){
				if (split1[i] != null || !"".equals(split1[i])){
					SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
					standLabitemMatchEntity.setLabitemId(Long.parseLong(split1[i]));
					standLabitemMatchEntity.setDicsubId(sdsStandDicsubId);
					SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
					if (standLabitemMatchEntity1 == null){
						//insert
						this.baseMapper.insert(standLabitemMatchEntity);
					}

				}
			}
		}else {
			//标准字典关联检验医嘱
			String[] split1 = laborderIds.split(",");
			for (int i = 0; i < split1.length; i++){
				if (split1[i] != null || !"".equals(split1[i])){
					SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
					standLabitemMatchEntity.setLaborderId(Long.parseLong(split1[i]));
					standLabitemMatchEntity.setDicId(sdsStandDicId);
					SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
					if (standLabitemMatchEntity1 == null){
						//insert
						this.baseMapper.insert(standLabitemMatchEntity);
					}

				}
			}
		}*/
		String[] split1 = laborderIds.split(",");
		String[] split2 = labitemIds.split(",");

		if (!StringUtils.isEmpty(sdsStandDicsubId)){
			for (String labitemId : split2){
				SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();

				standLabitemMatchEntity.setDicsubId(sdsStandDicsubId);
				standLabitemMatchEntity.setLabitemId(Long.valueOf(labitemId));
				standLabitemMatchEntity.setMatchType("H");

				sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
				sdsStandMatchLogEntity.setLabitemId(Long.valueOf(labitemId));

				sdsStandMatchLogEntity.setOperType(1);
				sdsStandMatchLogEntity.setMatchType("检验对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
				if (standLabitemMatchEntity1 == null){
					//insert
					this.baseMapper.insert(standLabitemMatchEntity);
					sdsStandMatchLogEntity.setOperTime(new Date());
					//更新日志
					sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
				}
			}

		}else {
			for (String laborderId : split1){
				SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();

				standLabitemMatchEntity.setDicId(sdsStandDicId);
				standLabitemMatchEntity.setLaborderId(Long.valueOf(laborderId));
				standLabitemMatchEntity.setMatchType("H");

				sdsStandMatchLogEntity.setDicId(sdsStandDicId);
				sdsStandMatchLogEntity.setLaborderId(Long.valueOf(laborderId));

				sdsStandMatchLogEntity.setOperType(1);
				sdsStandMatchLogEntity.setMatchType("检验对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
				if (standLabitemMatchEntity1 == null){
					//insert
					this.baseMapper.insert(standLabitemMatchEntity);
					sdsStandMatchLogEntity.setOperTime(new Date());
					//更新日志
					sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
				}
			}

		}



	}

	@Override
	public void undo(String labitemIds, Long sdsStandDicsubId, String laborderIds, Long sdsStandDicId, String username) {

		String[] split1 = laborderIds.split(",");
		String[] split2 = labitemIds.split(",");


        if (!StringUtils.isEmpty(sdsStandDicsubId)){

        	for (String labitemId : split2){
				SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();

				standLabitemMatchEntity.setDicsubId(sdsStandDicsubId);
				standLabitemMatchEntity.setLabitemId(Long.valueOf(labitemId));

				sdsStandMatchLogEntity.setDicsubId(sdsStandDicsubId);
				sdsStandMatchLogEntity.setLabitemId(Long.valueOf(labitemId));

				sdsStandMatchLogEntity.setOperType(2);
				sdsStandMatchLogEntity.setMatchType("检验对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
				this.baseMapper.deleteById(standLabitemMatchEntity1);
				sdsStandMatchLogEntity.setOperTime(new Date());
				sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
			}

		}else {
        	for (String laborderId : split1){
				SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
				SdsStandMatchLogEntity sdsStandMatchLogEntity = new SdsStandMatchLogEntity();

				standLabitemMatchEntity.setDicId(sdsStandDicId);
				standLabitemMatchEntity.setLaborderId(Long.valueOf(laborderId));

				sdsStandMatchLogEntity.setDicId(sdsStandDicId);
				sdsStandMatchLogEntity.setLaborderId(Long.valueOf(laborderId));

				sdsStandMatchLogEntity.setOperType(2);
				sdsStandMatchLogEntity.setMatchType("检验对照");
				sdsStandMatchLogEntity.setOperUser(username);
				SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
				this.baseMapper.deleteById(standLabitemMatchEntity1);
				sdsStandMatchLogEntity.setOperTime(new Date());
				sdsStandMatchLogMapper.insert(sdsStandMatchLogEntity);
			}

		}

    }

	@Override
	public void autoCompareLabitem() {
		List<SdsStandDicMatchParamEntity> standDicMatchEntities = sdsStandDicMatchParamService.getDicBySource(Arrays.asList("检验生化"));
		List<SdsStandDicMatchParamEntity> standDicsubMatchEntities = sdsStandDicMatchParamService.getDicsubBySource(Arrays.asList("检验生化"));
		//add 过滤检验医嘱医嘱项为空的
		List<CdcLabOrderEntity> cdcLabOrderEntities = cdcLabOrderMapper.selectList(new EntityWrapper<CdcLabOrderEntity>()
				.eq("is_active", 1)
				.isNotNull("order_mast_id")
		);
		List<CdcLabOrderDto> labOrderDtos = cdcLabOrderEntities.stream().map(t -> {
			CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
			BeanUtils.copyProperties(t, cdcLabOrderDto);
			return cdcLabOrderDto;
		}).collect(Collectors.toList());
		List<CdcLabItemEntity> cdcLabItemEntities = cdcLabitemMapper.selectList(new EntityWrapper<CdcLabItemEntity>().eq("is_active", 1));
		List<CdcLabitemDto> labitemDtos = cdcLabItemEntities.stream().map(t -> {
			CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
			BeanUtils.copyProperties(t, cdcLabitemDto);
			return cdcLabitemDto;
		}).collect(Collectors.toList());

		Map<Long, List<SdsStandDicMatchParamEntity>> dicMatchMap = new HashMap<>();
		Map<Long, List<SdsStandDicMatchParamEntity>> dicsubMatchMap = new HashMap<>();
		//将关键字
		//处理dic数据
		for (SdsStandDicMatchParamEntity standDicMatchEntity : standDicMatchEntities){
			List<SdsStandDicMatchParamEntity> dicMatchEntities = new ArrayList<>();
			if (CommonUtil.isEmpty(dicMatchMap.get(standDicMatchEntity.getDicId()))){
				//List<SdsStandDicMatchEntity> dicMatchEntities = new ArrayList<>();
				dicMatchEntities.add(standDicMatchEntity);
				//dicMatchMap.put(standDicMatchEntity.getDicId(), dicMatchEntities);
			}else {
				dicMatchEntities = dicMatchMap.get(standDicMatchEntity.getDicId());
				dicMatchEntities.add(standDicMatchEntity);
				//dicMatchMap.put(standDicMatchEntity.getDicId(), dicMatchEntities);
			}
			dicMatchMap.put(standDicMatchEntity.getDicId(), dicMatchEntities);
		}
		//处理dicsub数据
		/*for (SdsStandDicMatchParamEntity standDicMatchEntity : standDicsubMatchEntities){
			if (CommonUtil.isEmpty(dicsubMatchMap.get(standDicMatchEntity.getDicsubId()))){
				List<SdsStandDicMatchParamEntity> dicMatchEntities = new ArrayList<>();
				dicMatchEntities.add(standDicMatchEntity);
				dicsubMatchMap.put(standDicMatchEntity.getDicsubId(), dicMatchEntities);
			}else {
				List<SdsStandDicMatchParamEntity> dicMatchEntities = dicsubMatchMap.get(standDicMatchEntity.getDicsubId());
				dicMatchEntities.add(standDicMatchEntity);
			}
		}*/

		List<SdsStandLabitemMatchEntity> standLabitemMatchEntities = new ArrayList<>();
		//标准字典与检验医嘱对照
		Map<Long, List<CdcLabOrderDto>> labOrderMap = new HashMap<>();
		for (Long dicId : dicMatchMap.keySet()){
			List<SdsStandDicMatchParamEntity> dicMatchEntities = dicMatchMap.get(dicId);
			String filterTxt = "";
			List<CdcLabOrderDto> matchOrdmast = new ArrayList<>();
			a:for (SdsStandDicMatchParamEntity standDicMatchEntity : dicMatchEntities){
				String filterType = standDicMatchEntity.getFilterType();
				String matchTxt = standDicMatchEntity.getCompareContent();
				if (Objects.equals("O", filterType)){
					filterTxt = filterTxt + ":" + matchTxt + ",";
					continue a;
				}
				for (CdcLabOrderDto cdcLabOrderDto : labOrderDtos){
					String description = cdcLabOrderDto.getDescription();
					//处理并且关系的关键字:&分隔的关键字同时满足
					boolean checkFlg = true;
					if (matchTxt.contains("&")){
						String[] split = matchTxt.split("&");
							b:for (String s : split){
								if (!description.contains(s)){
									checkFlg = false;
									break b;
								}
							}
							if (checkFlg){
								cdcLabOrderDto.setMatchType("K");
								matchOrdmast.add(cdcLabOrderDto);
								labOrderMap.put(dicId, matchOrdmast);
							}
					}else {
							if (description.contains(matchTxt)){
								cdcLabOrderDto.setMatchType("K");
								matchOrdmast.add(cdcLabOrderDto);
								labOrderMap.put(dicId, matchOrdmast);
							}
					}
				}
			}
			//排除
			if (!StringUtils.isEmpty(filterTxt)){
				String[] split = filterTxt.split(",");
				List<CdcLabOrderDto> ordmastEntities = labOrderMap.get(dicId);
				Iterator<CdcLabOrderDto> it = ordmastEntities.iterator();
				while (it.hasNext()){
					CdcLabOrderDto ordmastEntity = it.next();
					for (String s : split){
						String[] split1 = s.split(":");
							if (ordmastEntity.getDescription().contains(split1[1])){
								it.remove();
							}
					}
				}
				labOrderMap.put(dicId, ordmastEntities);
			}
		}
		for (Long dicId : labOrderMap.keySet()){
			List<CdcLabOrderDto> ordmastEntities = labOrderMap.get(dicId);
			for (CdcLabOrderDto cdcOrdmastEntity : ordmastEntities){
				SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
				standLabitemMatchEntity.setDicId(dicId);
				standLabitemMatchEntity.setLaborderId(cdcOrdmastEntity.getId());
				SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
				if (StringUtils.isEmpty(standLabitemMatchEntity1)) {
					standLabitemMatchEntity.setMatchType(cdcOrdmastEntity.getMatchType());
					standLabitemMatchEntities.add(standLabitemMatchEntity);
				}
			}
		}

		//标准字典子项与检验项目对照
		Map<Long, List<CdcLabitemDto>> labitemMap = new HashMap<>();
		for (Long dicsubId : dicsubMatchMap.keySet()){
			List<SdsStandDicMatchParamEntity> dicMatchEntities = dicsubMatchMap.get(dicsubId);
			String filterTxt = "";
			List<CdcLabitemDto> matchOrdmast = new ArrayList<>();
			a:for (SdsStandDicMatchParamEntity standDicMatchEntity : dicMatchEntities){
				String filterType = standDicMatchEntity.getFilterType();
				String matchTxt = standDicMatchEntity.getCompareContent();
				if (Objects.equals("O", filterType)){
					filterTxt = filterTxt  + ":" + matchTxt + ",";
					continue a;
				}
				for (CdcLabitemDto labitemDto : labitemDtos){
					String description = labitemDto.getDescription();
					//处理并且关系的关键字:&分隔的关键字同时满足
					boolean checkFlg = true;
					if (matchTxt.contains("&")){
						String[] split = matchTxt.split("&");

							b:for (String s : split){
								if (!description.contains(s)){
									checkFlg = false;
									break b;
								}
							}
							if (checkFlg){
								labitemDto.setMatchType("K");
								matchOrdmast.add(labitemDto);
								labitemMap.put(dicsubId, matchOrdmast);
							}

					}else {

							if (description.contains(matchTxt)){
								labitemDto.setMatchType("K");
								matchOrdmast.add(labitemDto);
								labitemMap.put(dicsubId, matchOrdmast);
							}

					}
				}
			}
			//排除
			if (!StringUtils.isEmpty(filterTxt)){
				String[] split = filterTxt.split(",");
				List<CdcLabitemDto> ordmastEntities = labitemMap.get(dicsubId);
				Iterator<CdcLabitemDto> it = ordmastEntities.iterator();
				while (it.hasNext()){
					CdcLabitemDto ordmastEntity = it.next();
					for (String s : split){
						String[] split1 = s.split(":");

							if (ordmastEntity.getDescription().contains(split1[1])){
								it.remove();
							}


					}
				}
				labitemMap.put(dicsubId, ordmastEntities);
			}

		}
		for (Long dicsubId : labitemMap.keySet()){
			List<CdcLabitemDto> ordmastEntities = labitemMap.get(dicsubId);
			for (CdcLabitemDto cdcOrdmastEntity : ordmastEntities){
				SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
				standLabitemMatchEntity.setDicsubId(dicsubId);
				standLabitemMatchEntity.setLabitemId(cdcOrdmastEntity.getId());
				SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
				if (StringUtils.isEmpty(standLabitemMatchEntity1)) {
					standLabitemMatchEntity.setMatchType(cdcOrdmastEntity.getMatchType());
					standLabitemMatchEntities.add(standLabitemMatchEntity);
				}
			}
		}

		this.baseMapper.insertBatch(standLabitemMatchEntities);
	}

	@Override
	public List<CdcLabitemDto> pageLabitem(Page<CdcLabItemEntity> page, Map<String, Object> params) {
		Object dicSubId = params.get("dicSubId");
		Object dicId = params.get("dicId");
		String s = String.valueOf(dicSubId);
		String s2 = String.valueOf(dicId);
		String compare = (String) params.get("compare");
		String itemDescKey = params.get("description").toString();
		List<CdcLabitemDto> cdcLabitemDtoList = CollUtil.newArrayList();
		if ("0".equals(s)){
			return  cdcLabitemDtoList;
		}

		//根据检验项目与检验医嘱对照关系表查询检验项目
		List<Long> laborderIds = new ArrayList<>();
		List<Long> labItemIds = new ArrayList<>();
		if (!StringUtils.isEmpty(s2)){
			//关联检验医嘱
			List<SdsStandLabitemMatchEntity> labitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandLabitemMatchEntity>()
							.eq("dic_id", s2)
			);
			for (SdsStandLabitemMatchEntity sdsStandLabitemMatchEntity : labitemMatchEntities){
				Long laborderId = sdsStandLabitemMatchEntity.getLaborderId();
				laborderIds.add(laborderId);
			}

			if (null == laborderIds || laborderIds.size() ==0){
				return cdcLabitemDtoList;
			}
			List<CdcLabEntryEntity> cdcLabEntryEntities = cdcLabEntryMapper.selectByLaborderIds(laborderIds);
			for (CdcLabEntryEntity cdcLabEntryEntity : cdcLabEntryEntities){
				labItemIds.add(cdcLabEntryEntity.getLabItemId());
			}

			List<CdcLabItemEntity> cdcLabItemEntities = new ArrayList<>();
			Map<String, Object> map = new HashMap<>();
			map.put("dicsubId", s);
			List<Long> matchLabItemIds = this.baseMapper.selectMatchLabItemIds(map);
			map.clear();
			map.put("itemDescKey",itemDescKey);
			if (Objects.equals("all", compare)){
				map.put("labItemIds",labItemIds);
				cdcLabItemEntities = cdcLabitemMapper.selectByLabItemIds(page,map);
			}else {
				//差集
				labItemIds.removeAll(matchLabItemIds);
				map.put("labItemIds",labItemIds);
				cdcLabItemEntities = cdcLabitemMapper.selectByLabItemIds(page,map);
			}

			for (CdcLabItemEntity cdcLabitemEntity : cdcLabItemEntities){
				CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
				BeanUtil.copyProperties(cdcLabitemEntity,cdcLabitemDto);
				cdcLabitemDtoList.add(cdcLabitemDto);
			}

			List<SdsStandLabitemMatchEntity> sdsStandLabitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandLabitemMatchEntity>()
							.eq("dicsub_id", s)
			);
			//关联的数据进行勾选
			for (CdcLabitemDto cdcLabitemDto : cdcLabitemDtoList){
				for (SdsStandLabitemMatchEntity standLabitemMatchEntity : sdsStandLabitemMatchEntities){
					if (cdcLabitemDto.getId().equals(standLabitemMatchEntity.getLabitemId())){
						cdcLabitemDto.setIsCheck(true);
					}
				}
			}

			/*List<CdcLabItemEntity> cdcLabItemEntities = cdcLabitemMapper.selectByLabItemIds(page, labItemIds);
			for (CdcLabItemEntity cdcLabitemEntity : cdcLabItemEntities){
				CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
				BeanUtil.copyProperties(cdcLabitemEntity,cdcLabitemDto);
				cdcLabitemDtoList.add(cdcLabitemDto);
			}*/

		}

		return cdcLabitemDtoList;

		//查询检验项目表所有数据
		/*List<CdcLabItemEntity> page1 = cdcLabitemMapper.page(page, params);
		for (CdcLabItemEntity cdcLabitemEntity : page1){
			CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
			BeanUtil.copyProperties(cdcLabitemEntity,cdcLabitemDto);
			cdcLabitemDtoList.add(cdcLabitemDto);
		}*/

		/*if ((dicSubId != null) && (!"".equals(dicSubId))){
			//点击标准化字典子项
			List<SdsStandLabitemMatchEntity> labitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandLabitemMatchEntity>()
							.eq("dicsub_id", s)
			);
			//关联的数据进行勾选
			for (CdcLabitemDto cdcLabitemDto : cdcLabitemDtoList){
				for (SdsStandLabitemMatchEntity standLabitemMatchEntity : labitemMatchEntities){
					if (cdcLabitemDto.getId().equals(standLabitemMatchEntity.getLabitemId())){
						cdcLabitemDto.setIsCheck(true);
					}
				}
			}
			Object o = params.get("compare");
			String compare = String.valueOf(o);
			//已对照-yes，未对照-no，全部
			List<CdcLabitemDto> collect = new ArrayList<>();
			if("yes".equals(compare)){
				collect = cdcLabitemDtoList.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
				return collect;
			}else if("no".equals(compare)){
				collect = cdcLabitemDtoList.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
				return collect;
			}else {
				return cdcLabitemDtoList;
			}

		}else {
			//不点击默认查询所有
			return cdcLabitemDtoList;
		}*/

	}

	@Override
	public List<CdcLabOrderDto> pageLaborder(Page<CdcLabOrderEntity> page, Map<String, Object> params) {
		Object dicId = params.get("dicId");
		String s = String.valueOf(dicId);
		Object dicSubId = params.get("dicSubId");
		String compare = (String) params.get("compare");
		if ("0".equals(s)){
			return null;
		}

		List<CdcLabOrderEntity> page1 = new ArrayList<>();
		List<CdcLabOrderDto> cdcLabOrderDtoList = CollUtil.newArrayList();
		List<SdsStandLabitemMatchEntity> sdsStandLabitemMatchEntities = new ArrayList<>();
		if (!StringUtils.isEmpty(dicSubId)){
			String s1 = String.valueOf(dicSubId);
			params.put("dicsubId", s1);
			sdsStandLabitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandLabitemMatchEntity>()
							.eq("dicsub_id", s)
			);
		}else {
			String s1 = String.valueOf(dicId);
			params.put("dicId", s1);
			sdsStandLabitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandLabitemMatchEntity>()
							.eq("dic_id", s)
			);
		}

		if (Objects.equals("all", compare)){
			page1 = cdcLabOrderMapper.page(page, params);
		}else {
			List<Long> matchLabOrderIds = this.baseMapper.selectMatchLabOrderIds(params);
			page1 = cdcLabOrderMapper.pageExcludeMatch(page, params, matchLabOrderIds);
		}

		for (CdcLabOrderEntity cdcLabOrderEntity : page1){
			//add 过滤检验医嘱医嘱项为空的
			if (StringUtils.isEmpty(cdcLabOrderEntity.getOrderMastId())){
				continue;
			}
			CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
			BeanUtil.copyProperties(cdcLabOrderEntity, cdcLabOrderDto);
			if (!StringUtils.isEmpty(cdcLabOrderDto.getOrderMastId())){
				CdcOrdmastEntity ordmastEntity = cdcOrdmastMapper.selectById(cdcLabOrderDto.getOrderMastId());
				cdcLabOrderDto.setOrderMastDesc(ordmastEntity.getDescription());
			}
			cdcLabOrderDtoList.add(cdcLabOrderDto);
		}

		//关联的数据进行勾选
		for (CdcLabOrderDto cdcLabOrderDto : cdcLabOrderDtoList){
			for (SdsStandLabitemMatchEntity standLabitemMatchEntity : sdsStandLabitemMatchEntities){
				if (cdcLabOrderDto.getId().equals(standLabitemMatchEntity.getLaborderId())){
					cdcLabOrderDto.setIsCheck(true);
				}
			}
		}


		return cdcLabOrderDtoList;
		/*List<CdcLabOrderEntity> page1 = cdcLabOrderMapper.page(page, params);
		List<CdcLabOrderDto> cdcLabOrderDtoList = CollUtil.newArrayList();
		for (CdcLabOrderEntity cdcLabOrderEntity : page1){
			CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
			BeanUtil.copyProperties(cdcLabOrderEntity,cdcLabOrderDto);
			*//*CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryMapper.selectById(cdcLabOrderDto.getBusLabCateId());
			cdcLabOrderDto.setLabCateDesc(cdcDictionaryEntity.getDescription());*//*
			if (!StringUtils.isEmpty(cdcLabOrderDto.getOrderMastId())){
				CdcOrdmastEntity ordmastEntity = cdcOrdmastMapper.selectById(cdcLabOrderDto.getOrderMastId());
				cdcLabOrderDto.setOrderMastDesc(ordmastEntity.getDescription());
			}else {
				cdcLabOrderDto.setOrderMastDesc("");
			}
			cdcLabOrderDtoList.add(cdcLabOrderDto);
		}

		if ((s != null) && (!"".equals(s))){
			//点击标准化字典
			List<SdsStandLabitemMatchEntity> labitemMatchEntities = this.baseMapper.selectList(
					new EntityWrapper<SdsStandLabitemMatchEntity>()
							.eq("dic_id", s)
			);
			//关联的数据进行勾选
			for (CdcLabOrderDto cdcLabOrderDto : cdcLabOrderDtoList){
				for (SdsStandLabitemMatchEntity standLabitemMatchEntity : labitemMatchEntities){
					if (cdcLabOrderDto.getId().equals(standLabitemMatchEntity.getLaborderId())){
						cdcLabOrderDto.setIsCheck(true);
					}
				}
			}
		}

		//已对照-yes，未对照-no，全部
		Object o = params.get("compare");
		String compare = String.valueOf(o);
		List<CdcLabOrderDto> collect = new ArrayList<>();
		if("yes".equals(compare)){
			collect = cdcLabOrderDtoList.stream().filter(item -> (item.getIsCheck() != null && item.getIsCheck() == true)).collect(Collectors.toList());
			return collect;
		}else if("no".equals(compare)){
			collect = cdcLabOrderDtoList.stream().filter(item -> (item.getIsCheck() == null || "".equals(item.getIsCheck()))).collect(Collectors.toList());
			return collect;
		}else {
			return cdcLabOrderDtoList;
		}*/


	}

	@Override
	public void autoCompareLaborder() {
		//查询标准检验字典数据
		List<SdsStandDicEntity> sdsStandDicEntities = sdsStandDicService.listBySource("检验");
		//查询检验医嘱数据
		List<CdcLabOrderEntity> cdcLabOrderEntities = cdcLabOrderMapper.selectList(null);

		/*for (SdsStandDicEntity standDicEntity : sdsStandDicEntities){
			for (CdcLabOrderEntity cdcLabOrderEntity : cdcLabOrderEntities){
				//自动匹配：检验代码匹配
				if (standDicEntity.getDicCatCode().equals(cdcLabOrderEntity.getCode())){
					SdsStandLabitemMatchEntity standLabitemMatchEntity = new SdsStandLabitemMatchEntity();
					standLabitemMatchEntity.setLaborderId(cdcLabOrderEntity.getId());
					standLabitemMatchEntity.setDicId(standDicEntity.getId());
					SdsStandLabitemMatchEntity standLabitemMatchEntity1 = this.baseMapper.selectOne(standLabitemMatchEntity);
					if (standLabitemMatchEntity1 == null){
						//不存在insert
						this.baseMapper.insert(standLabitemMatchEntity);
					}
				}
			}

		}*/
	}

	@Override
	public List<CdcLabOrderDto> pageMatchLaborder(Page<SdsStandLabitemMatchEntity> page, Map<String, Object> params) {
		Object dicId = params.get("dicId");
		Object dicSubId = params.get("dicSubId");
		if (StringUtils.isEmpty(dicSubId) && StringUtils.isEmpty(dicId)){
			return null;
		}

		List<CdcLabOrderDto> cdcLabOrderDtoList = CollUtil.newArrayList();
		if (!StringUtils.isEmpty(dicSubId)){
			//点击标准化字典子项
			String s = String.valueOf(dicSubId);
			params.put("dicsubId", s);
		}else if (!StringUtils.isEmpty(dicId)){
			String s = String.valueOf(dicId);
			params.put("dicId", s);
		}
		List<SdsStandLabitemMatchEntity> page1 = this.baseMapper.page(page, params);
		if (page1.size() != 0){
			for (SdsStandLabitemMatchEntity standLabitemMatchEntity : page1){
				CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
				CdcLabOrderEntity cdcLabOrderEntity = cdcLabOrderMapper.selectById(standLabitemMatchEntity.getLaborderId());
				//add 过滤检验医嘱医嘱项为空的
				if (StringUtils.isEmpty(cdcLabOrderEntity.getOrderMastId())){
					continue;
				}
				BeanUtil.copyProperties(cdcLabOrderEntity,cdcLabOrderDto);
				if (!StringUtils.isEmpty(cdcLabOrderDto.getOrderMastId())){
					CdcOrdmastEntity ordmastEntity = cdcOrdmastMapper.selectById(cdcLabOrderDto.getOrderMastId());
					cdcLabOrderDto.setOrderMastDesc(ordmastEntity.getDescription());
				}
				cdcLabOrderDto.setIsCheck(true);
				cdcLabOrderDtoList.add(cdcLabOrderDto);
			}
		}

		return cdcLabOrderDtoList;
	}

	@Override
	public List<CdcLabitemDto> pageMatchLabitem(Page<SdsStandLabitemMatchEntity> page, Map<String, Object> params) {
		Object dicSubId = params.get("dicSubId");
		if (StringUtils.isEmpty(dicSubId)){
			return null;
		}
		String itemDescKey = params.get("description").toString();
		List<CdcLabitemDto> cdcExamdicDtos = CollUtil.newArrayList();
		String s = String.valueOf(dicSubId);
		params.put("dicId", "");
		params.put("dicsubId", s);
		List<SdsStandLabitemMatchEntity> page1 = this.baseMapper.page(page, params);
		//默认查询已对照医嘱，未对照返回空
		if (page1.size() != 0){
			for (SdsStandLabitemMatchEntity standLabitemMatchEntity : page1){
				CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
				CdcLabItemEntity cdcLabItemEntity = cdcLabitemMapper.selectById(standLabitemMatchEntity.getLabitemId());
				if (!cdcLabItemEntity.getDescription().contains(itemDescKey)) {
					continue;
				}
				BeanUtil.copyProperties(cdcLabItemEntity, cdcLabitemDto);
				cdcLabitemDto.setIsCheck(true);
				cdcExamdicDtos.add(cdcLabitemDto);
			}
		}

		return cdcExamdicDtos;
	}

	@Override
	public List<Long> selectMatchDicsub() {
		return this.baseMapper.selectMatchDicsub();
	}

}